/*
	URAT 串口

	引脚：TxD0 GPH2
	      RxD0 GPH3

	yum install -y man-pages

	man putc
	man getc
*/

#include "s3c2440_soc.h"
#include "uart.h"

// 115200, 8n1
void urat0_init()
{
	/* 设置引脚用于串口 */
	// 清零
	GPHCON &= ~((3<<4) | (3<<6));
	// 0b10
	GPHCON |= ((2<<4) | (2<<6));
	// 清零, 使能上拉
	GPHUP  &= ((1<<2) | (1<<3));

	/* 设置波特率 */
	// UBRDIVn = (int)( UART clock / ( buad rate x 16) ) –1
	// UART clock = PCLK = 50M
	// UBRDIVn = (int)( 50000000 / ( 115200 x 16) ) –1 = 26
	UCON0 = 0x00000005;
	UBRDIV0 = 26;

	// 设置数据格式
	ULCON0 = 0x00000003;
}

 void putchar(int c)
 {
	 // 判断是否为空
	 while(!(UTRSTAT0 & (1<<2)));

	 UTXH0 = (unsigned char)c;
 }

int getchar(void)
{
	 while(!(UTRSTAT0 & (1<<0)));

	 return URXH0;
}

void puts(const char *s)
{
	while(*s)
	{
		putchar(*s++);
	}
}



